package com.atguigu.admin.controller;

import com.atguigu.admin.bean.City;
import com.atguigu.admin.bean.Dept;
import com.atguigu.admin.bean.User;
import com.atguigu.admin.service.impl.CityServiceImpl;
import com.atguigu.admin.service.impl.CityServiceImpl;
import com.atguigu.admin.service.impl.DeptServiceImpl;
import com.atguigu.admin.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * ClassName: IndexController
 * Package: com.atguigu.admin.controller
 * Description:
 *
 * @Author Root
 * @Create 2023/3/31 16:31
 * @Version 1.0
 */
@Controller
@Slf4j
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DeptServiceImpl deptService;
    @Autowired
    private CityServiceImpl cityService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @ResponseBody
    @GetMapping("/city")
    public City getCityById(@RequestParam("id") Long id){
        City city = cityService.findById(id);
        return city;
    }
    @ResponseBody
    @GetMapping("/dept")
    public Dept getDeptById(){
        Dept dept = deptService.getDeptById(1);
        return dept;
    }
    @ResponseBody
    @GetMapping("/sql")
    public String queryFromDb(){
        String sql="select count(*) from t_book";
        Long count = jdbcTemplate.queryForObject(sql, Long.class);
        return count.toString();
    }

    /**
     * 来登录页
     * @return
     */
    @GetMapping({"/","/login"})
    public String loginPage(){
        return "login";
    }


    @PostMapping("/login")
    public String main( User user, HttpSession session, Model model){
        if(StringUtils.hasLength(user.getUserName()) && StringUtils.hasLength(user.getPassword())){
            //把登陆成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登录成功重定向到main.html;  重定向防止表单重复提交
            return "redirect:/main.html";
        }else {
            model.addAttribute("msg","账号密码错误");
            //回到登录页面
            return "login";
        }
    }

    @GetMapping("/main.html")
    public String mainPage(HttpSession session,Model model){

        log.info("当前方式是:{}","mainPage");
   /*     Object loginUser = session.getAttribute("loginUser");
        if(loginUser!=null){
            return "main";
        }else {
            return "login";
        }*/
        String s = redisTemplate.opsForValue().get("/main.html;jsessionid=D211CBB0E7357D7A0C74015D536DBAF0");
        String s1 = redisTemplate.opsForValue().get("/sql");
        model.addAttribute("mainCount",s);
        model.addAttribute("sqlCount",s1);
        return "main";
    }

}
